<?php

namespace app\admin\controller;

use think\Db;

class Turntable extends Common
{
    public function index()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types) {
            $where['types'] = $types;
        }

        $data = Db::name('turn_table')->where($where)->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    public function add()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (isset($post['money']) && ($post['money'] + 0)) {
                $post['reward'] = 1;
            } else {
                $post['reward'] = 0;
            }

            if (!$id) {
                $res = Db::name('turn_table')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('turn_table')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('turn_table')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function del()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('turn_table')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
    public function objects()
    {
        $data = Db::name('turn_table_of_objects')
        ->alias('a')
        ->join('mall_goods b', 'a.goods_id=b.id', 'left')
        ->field('a.*,b.name,b.image')
        ->order('a.sort desc, a.id desc')
        ->paginate(_getCachePages(), false, ['query' => request()->param()])
        ->each(function ($item)
        {
            $item['name'] = $item['name'] ?? '谢谢惠顾';
            $item['image'] = $item['image'] ?? '/disk/uploads/image/20220720/5c3beb82429563d843837fe3226e190075f48b2a.png';
            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addobjects()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');
            if ($post['goods_id']) {
                $goods = Db::name('mall_goods')->where(['id'=>$post['goods_id']])->find();
                if (!$goods) {
                    return ['err' => '商品不存在'];
                }
            }

            if (!$id) {
                $res = Db::name('turn_table_of_objects')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('turn_table_of_objects')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('turn_table_of_objects')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function delobjects()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('turn_table_of_objects')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
    public function topuprecords()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            if ($search_type == 'user_id') {
                $where['a.'.$search_type] = $search_name;
            } else {
                $where['b.'.$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $status = input('status');
        if ($status || $status === '0') {
            $where['status'] = $status;
        }

        $data = Db::name('user_lottery')
        ->alias('a')
        ->join('user c', 'a.user_id=c.id', 'left')
        ->where($where)
        ->field('a.*,c.name username')
        ->order('a.create_time desc')
        ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        $this->assign('status', $status);

        return $this->fetch();
    }
    public function records()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            if ($search_type == 'user_id') {
                $where['a.'.$search_type] = $search_name;
            } else {
                $where['b.'.$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $status = input('status');
        if ($status || $status === '0') {
            $where['status'] = $status;
        }

        $data = Db::name('user_lottery_of_objects')
        ->alias('a')
        ->join('mall_goods b', 'a.goods_id=b.id', 'left')
        ->join('user c', 'a.user_id=c.id', 'left')
        ->where($where)
        ->field('a.*,b.name,b.image,c.name username')
        ->order('a.create_time desc')
        ->paginate(_getCachePages(), false, ['query' => request()->param()])
        ->each(function ($item)
        {
            $item['name'] = $item['name'] ?? '商品已失效';
            $item['image'] = $item['image'] ?? '商品已失效';
            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        $this->assign('status', $status);

        return $this->fetch();
    }
    public function objectRecords()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            if ($search_type == 'user_id') {
                $where['a.'.$search_type] = $search_name;
            } else {
                $where['a.'.$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $status = input('status');
        if ($status || $status === '0') {
            if ($status == 1) {
                $where['goods_id'] = ['<>', 0];
            } else {
                $where['goods_id'] = 0;
            }
        }
        $this->assign('status', $status);

        $paymethod = input('paymethod');
        if ($paymethod) {
            $where['type'] = $paymethod;
        }
        $this->assign('paymethod', $paymethod);



        $data = Db::name('turn_table_of_objects_records')
        ->alias('a')
        ->join('mall_goods b', 'a.goods_id=b.id', 'left')
        ->join('user c', 'a.user_id=c.id', 'left')
        ->where($where)
        ->field('a.*,b.name,b.image,c.name username')
        ->order('a.time desc')
        ->paginate(_getCachePages(), false, ['query' => request()->param()])
        ->each(function ($item)
        {
            $item['name'] = $item['name'] ?? '商品已失效';
            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

}
